<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 *  ======================================= 
 *  Author     : Muhammad Surya Ikhsanudin 
 *  License    : Protected 
 *  Email      : mutofiyah@gmail.com 
 *   
 *  Dilarang merubah, mengganti dan mendistribusikan 
 *  ulang tanpa sepengetahuan Author 
 *  ======================================= 
 */
require_once APPPATH . "/third_party/PHPExcel.php";

class Excel extends PHPExcel {

    public function __construct() {
        parent::__construct();
    }

    public static function Output($data) {
        switch ($data['FK_Ma_dmMauDon']) {
            case 0 :
                Excel::clccgdqp($data);
                break;
            case 1 :
                Excel::cltbhyt($data);
                break;
            case 2 :
                Excel::cnsv($data);
                break;
            case 3 :
                Excel::dbl($data);

                break;
            case 4 :
                Excel::mvv($data);
                break;
            case 5 :

                break;
            case 6 :

                break;





            default :
                break;
        }
    }

    /*
     * OK 2
     */

    public static function cnsv($data) {

        $objTpl = PHPExcel_IOFactory::load("public/doc/chung_nhan_sinh_vien.xls");
        $objTpl->setActiveSheetIndex(0);
        $objTpl->getActiveSheet()->setCellValue('A6', "Chứng nhận anh/chi: " . $data['HoTen_SV']);
        $objTpl->getActiveSheet()->setCellValue('A7', "Sinh ngày: " . $data['NgaySinh_SV']);
        $objTpl->getActiveSheet()->setCellValue('E7', "Số thẻ HSSV: " . $data['Ma_SV']);
        $objTpl->getActiveSheet()->setCellValue('A8', "Ngành hoc: " . $data['Nganh_SV']);
        $objTpl->getActiveSheet()->setCellValue('D9', "Khóa học: " . $data['KhoaHoc_SV']);
        $objTpl->getActiveSheet()->setCellValue('A10', "Hệ đào tạo : " . $data['HeDaoTao_SV']);
        $objTpl->getActiveSheet()->setCellValue('A9', "Lớp: " . $data['Lop_SV']);

        $objTpl->getActiveSheet()->setTitle($data['Ma_SV']);



        $filename = "CNSV-" . $data['Ma_SV'] . '.xls';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');
        $objWriter->save('php://output');
        return $objTpl;
    }

// key 4
    public static function mvv($data) {

        $objTpl = PHPExcel_IOFactory::load("public/doc/mau_vay_von.xls");
        $objTpl->setActiveSheetIndex(0);  //set first sheet as active
        $objTpl->getActiveSheet()->setCellValue('A6', "Họ và tên : " . $data['HoTen_SV']);
        $objTpl->getActiveSheet()->setCellValue('A7', "Sinh ngày: " . $data['NgaySinh_SV']);
        $objTpl->getActiveSheet()->setCellValue('E7', "Giới tính: " . $data['GioiTinh_SV']);
        $objTpl->getActiveSheet()->setCellValue('A8', "CMND số: " . $data['CMND_SV']);
        $objTpl->getActiveSheet()->setCellValue('D13', "Loai hình đào tạo: " . $data['LoaiHinhDaoTao']);
        $objTpl->getActiveSheet()->setCellValue('D14', "Số thẻ HSSV: " . $data['Ma_SV']);
        $objTpl->getActiveSheet()->setCellValue('A11', "Ngành hoc: " . $data['Nganh_SV']);
        $objTpl->getActiveSheet()->setCellValue('A13', "Khóa học: " . $data['KhoaHoc_SV']);
        $objTpl->getActiveSheet()->setCellValue('A12', "Hệ đào tạo : " . $data['HeDaoTao']);
        $objTpl->getActiveSheet()->setCellValue('A14', "Lớp: " . $data['Lop_SV']);
        $objTpl->getActiveSheet()->setCellValue('A15', "Khoa: " . $data['KhoaHoc_SV']);
        $objTpl->getActiveSheet()->setCellValue('A16', "Ngày nhập hoc: " . $data['NgayNhapHoc']);
        $objTpl->getActiveSheet()->setCellValue('A19', "Thuộc diện: " . $data['ThuocDien']);
        $objTpl->getActiveSheet()->setCellValue('A20', "Thuộc đối tượng: " . $data['DoiTuong']);
        $objTpl->getActiveSheet()->setCellValue('E16', "Thời gian ra trường (tháng/năm): " . $data['ThoiGianHoc']);
        $objTpl->getActiveSheet()->setCellValue('A22', "Trong thời gian theo học tại trường, anh (chị) " . $data['HoTen_SV'] . " không bị xử phạt hành chính trở lên về các hành vi: cờ bạc, nghiện hút, trộm cắp, buôn lậu.");


        $objTpl->getActiveSheet()->setTitle($data['Ma_SV']);



        $filename = "MVV-" . $data['Ma_SV'] . '.xls'; //just some random filename
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
        $objWriter->save('php://output');  //send it to user, of course you can save it to disk alshocjHoD  
        return $objTpl;
    }

    /*
     * key ok 0
     */

    public static function clccgdqp($data) {


        $objTpl = PHPExcel_IOFactory::load("public/doc/cap_lai_chung_chi_gdqp.xls");
        $objTpl->setActiveSheetIndex(0);  //set first sheet as active
        $objTpl->getActiveSheet()->setCellValue('A11', "Tên em là: " . $data['HoTen_SV']);
        $objTpl->getActiveSheet()->setCellValue('A12', "Sinh ngày: " . $data['NgaySinh_SV']);
        $objTpl->getActiveSheet()->setCellValue('A15', "Số thẻ HSSV: " . $data['Ma_SV']);

        $objTpl->getActiveSheet()->setCellValue('D13', "Khoa: " . $data['Khoa_SV']);
        $objTpl->getActiveSheet()->setCellValue('A18', "Lý do xin cấp lại: " . $data['LyDo']);
        $objTpl->getActiveSheet()->setCellValue('A13', "là sinh viên lớp: " . $data['Lop_SV']);

        $objTpl->getActiveSheet()->setTitle($data['Ma_SV']);



        $filename = "DCLCC_GDQP-" . $data['Ma_SV'] . '.xls'; //just some random filename
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
        $objWriter->save('php://output');  //send it to user, of course you can save it to disk alshocjHoD  
        return $objTpl;
    }

    /*
     * key ok 1
     */

    public static function cltbhyt($data) {

        $objTpl = PHPExcel_IOFactory::load("public/doc/cap_lai_the_bhyt.xls");
        $objTpl->setActiveSheetIndex(0);  //set first sheet as active
        $objTpl->getActiveSheet()->setCellValue('A11', "Họ và tên: " . $data['HoTen_SV']);
        $objTpl->getActiveSheet()->setCellValue('F25', $data['HoTen_SV']);
        $objTpl->getActiveSheet()->setCellValue('A12', "Sinh ngày: " . $data['NgaySinh_SV']);
        $objTpl->getActiveSheet()->setCellValue('F11', "Giới tính: " . $data['GioiTinh_SV']);
        $objTpl->getActiveSheet()->setCellValue('A16', "Số thẻ BHYT: " . $data['SoBH']);
        $objTpl->getActiveSheet()->setCellValue('A14', "Khoa: " . $data['Khoa_SV']);
        $objTpl->getActiveSheet()->setCellValue('A17', "Lý do cấp lại thẻ BHYT:  " . $data['LyDo']);
        $objTpl->getActiveSheet()->setCellValue('A15', "Lớp: " . $data['Lop_SV']);

        $objTpl->getActiveSheet()->setTitle($data['Ma_SV']);



        $filename = "CB-BHYT-" . $data['Ma_SV'] . '.xls'; //just some random filename
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
        $objWriter->save('php://output');  //send it to user, of course you can save it to disk alshocjHoD  
        return $objTpl;
    }

    public static function dbl($data) {

        $objTpl = PHPExcel_IOFactory::load("public/doc/don_bao_luu_hoc_tap.xls");
        $objTpl->setActiveSheetIndex(0);  //set first sheet as active
        $objTpl->getActiveSheet()->setCellValue('A10', "Tên em là: " . $data['HoTen_SV']);
        $objTpl->getActiveSheet()->setCellValue('A11', "Sinh ngày: " . $data['NgaySinh_SV']);





        $objTpl->getActiveSheet()->setCellValue('G14', "Ngành hoc: " . $data['NganhHoc_SV']);
        $objTpl->getActiveSheet()->setCellValue('A15', "Khóa học: " . $data['KhoaHoc_SV']);
        $objTpl->getActiveSheet()->setCellValue('A15', "Khoa: " . $data['Khoa_SV']);
        $objTpl->getActiveSheet()->setCellValue('A14', "Em hiện là sinh viên lớp: " . $data['Lop_SV']);
        $objTpl->getActiveSheet()->setCellValue('H46', "Lớp: " . $data['Lop_SV']);

        $objTpl->getActiveSheet()->setTitle($data['Ma_SV']);



        $filename = "CNSV-" . $data['Ma_SV'] . '.xls'; //just some random filename
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
        $objWriter->save('php://output');  //send it to user, of course you can save it to disk alshocjHoD  
        return $objTpl;
    }

    public static function cktn($data) {

        $objTpl = PHPExcel_IOFactory::load("public/doc/chung_nhan_sinh_vien.xls");
        $objTpl->setActiveSheetIndex(0);  //set first sheet as active
        $objTpl->getActiveSheet()->setCellValue('A6', "Chứng nhận anh/chi: " . $data['HoTen_SV']);
        $objTpl->getActiveSheet()->setCellValue('A7', "Sinh ngày: " . $data['NgaySinh_SV']);
        $objTpl->getActiveSheet()->setCellValue('E7', "Số thẻ HSSV: " . $data['Ma_SV']);
        $objTpl->getActiveSheet()->setCellValue('A8', "Ngành hoc: " . $data['Nganh_SV']);
        $objTpl->getActiveSheet()->setCellValue('D9', "Khóa học: " . $data['KhoaHoc_SV']);
        $objTpl->getActiveSheet()->setCellValue('A10', "Hệ đào tạo : " . $data['HeDaoTao_SV']);
        $objTpl->getActiveSheet()->setCellValue('A9', "Lớp: " . $data['Lop_SV']);

        $objTpl->getActiveSheet()->setTitle($data['Ma_SV']);



        $filename = "CNSV-" . $data['Ma_SV'] . '.xls'; //just some random filename
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
        $objWriter->save('php://output');  //send it to user, of course you can save it to disk alshocjHoD  
        return $objTpl;
    }

    public static function xuatfile($data) {

        $objTpl = PHPExcel_IOFactory::load('public/doc/' . $data['TenFile']);
        $objTpl->setActiveSheetIndex(0);
        $ArrayNoiDung = common::StringToArray($data['NoiDung']);
        if (is_array($ArrayNoiDung)) {
            foreach ($ArrayNoiDung as $key => $value) {
                $objTpl->getActiveSheet()->setCellValue($key, $value);
            }
        }


        $filename = $data['TenFile'];
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');

        $objWriter = PHPExcel_IOFactory::createWriter($objTpl, 'Excel5');
        $objWriter->save('php://output');
    }

    public static function xuatfile2($data) {
        if (count($data) > 0) {
            $xls = PHPExcel_IOFactory::load('public/doc/' . $data[0]['TenFile']);
            $xls->removeSheetByIndex();
            foreach ($data as $key => $row) {

                $objTpl = PHPExcel_IOFactory::load('public/doc/' . $row['TenFile']);
                $ArrayNoiDung = common::StringToArray($row['NoiDung']);
                if (isset($ArrayNoiDung)) {
                    foreach ($ArrayNoiDung as $ViTriO => $value) {
                        $objTpl->getActiveSheet()->setCellValue($ViTriO, $value);
                    }
                }
                $sheet = $objTpl->getActiveSheet();
                $sheet->setTitle('A' . $key);
                $xls->addSheet($sheet, $key);
            }

            $filename = 'DS_LAY_DAU.xls';
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $filename . '"');
            header('Cache-Control: max-age=0');

            $objWriter = PHPExcel_IOFactory::createWriter($xls, 'Excel5');
            $objWriter->save('php://output');
        }
    }

}
